//过度方法
export const scrollToElement = (element: any) => {
  const elementRect = element.getBoundingClientRect()
  const elementTop = elementRect.top
  const offsetPosition = elementTop + window.scrollY // 修正页面可能存在的垂直滚动偏移
  const start = window.scrollY
  const distance = offsetPosition - start
  const duration = 1000 // 动画持续时间，单位毫秒
  let startTime: any = null

  function animation(currentTime: any) {
    if (startTime === null) startTime = currentTime
    const timeElapsed = currentTime - startTime
    const nextStep = easeInOutQuad(timeElapsed, start, distance, duration)

    window.scrollTo(0, nextStep)
    if (timeElapsed < duration) {
      window.requestAnimationFrame(animation)
    }
  }

  function easeInOutQuad(t: any, b: any, c: any, d: any) {
    t /= d / 2
    if (t < 1) return (c / 2) * t * t + b
    t--
    return (-c / 2) * (t * (t - 2) - 1) + b
  }

  window.requestAnimationFrame(animation)
}
